package com.example.demo.controller;

import com.example.demo.model.Books;

import com.example.demo.repository.BooksRepository;

import com.example.demo.service.BooksService;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.data.domain.Page;

import org.springframework.stereotype.Controller;

import org.springframework.ui.Model;

import org.springframework.web.bind.annotation.*;

import java.util.List;
@Controller
public class BooksController {

    @Autowired
    private BooksService bookService;
    //首页
    @GetMapping("/")
    public String index(Model model){
        return findPaginated(1,"name","asc", model);

    }
    @GetMapping("/showNewBookForm")
    public String showNewBookForm(Model model){
        Books book = new Books();
        model.addAttribute("book",book);
        return "new_book";
    }
    //新增图书页
    @PostMapping("/addBook")
    public String addBook(@ModelAttribute("book") Books book){
        //将图书保存到数据库
        bookService.addBook(book);
        return "redirect:/";
    }
    //更新图书信息页
    @GetMapping("/updateBook/{id}")
    public String updateBook(@PathVariable(value = "id") long id, Model model){
        //获取id
        Books book = bookService.getBookById(id);
        //填充表单
        model.addAttribute("book", book);
        return "update_book";
    }
    //删除指定id
    @GetMapping("/deleteBook/{id}")
    public String deleteBook(@PathVariable(value = "id") long id){
        this.bookService.deleteBookById(id);
        return "redirect:/";
    }
    //分页
    @GetMapping("/page/{pageNo}")
    public String findPaginated(@PathVariable(value = "pageNo") int pageNo,
                                @RequestParam("sortField") String sortField,
                                @RequestParam("sortDir") String sortDir,
                                Model model){
        int pageSize = 4;
        Page<Books> page = bookService.findPaginated(pageNo, pageSize, sortField, sortDir);
        List<Books> listBook = page.getContent();
        model.addAttribute("currentPage", pageNo);
        model.addAttribute("totalPages", page.getTotalPages());
        model.addAttribute("totalItems", page.getTotalElements());
        model.addAttribute("sortField", sortField);
        model.addAttribute("sortDir", sortDir);
        model.addAttribute("reverseSortDir", sortDir.equals("asc") ? "desc" : "asc");
        model.addAttribute("listBook",listBook);
        return "index";
    }
    @Autowired
    private BooksRepository bookRepository;
    //获取所有书
    @GetMapping("/books")
    public String getBooks(Model model){
        List<Books> books = bookRepository.findAll();
        model.addAttribute("books",books);
        return "search";
    }
    //    //查询作者
    @PostMapping("/books/search")
    public String searchBook(@RequestParam("author") String author, Model model) {
        List<Books> books = bookRepository.findByAuthor(author);
        model.addAttribute("books",books);
        return "search";
    }


}